package com.example.park_management.utils;

import java.util.Arrays;
import java.util.List;

public class CircularLinkedList_lh {
    private Node head;
    private Node tail;
    private int size;

    private class Node {
        private Long data;
        private Node next;

        public Node(Long data) {
            this.data = data;
            this.next = null;
        }
    }

    public CircularLinkedList_lh() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }

    public void addAll(List<Long> list) {
        if (list == null || list.isEmpty()) {
            return;
        }

        for (Long data : list) {
            add(data);
        }
    }

    public void add(Long data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            tail = newNode;
            tail.next = head;
        } else {
            tail.next = newNode;
            tail = newNode;
            tail.next = head;
        }
        size++;
    }

    public Long next() {
        if (head == null) {
            throw new IllegalStateException("The list is empty.");
        }

        Long data = head.data;
        head = head.next;
        return data;
    }

    public int getSize() {
        return size;
    }

    public static void main(String[] args) {
        List<Long> dataList = Arrays.asList(1L, 2l, 3l, 4l, 5l);
        CircularLinkedList_lh circularLinkedListLh = new CircularLinkedList_lh();
        circularLinkedListLh.addAll(dataList);

        // 循环遍历链表，每次调用next方法都能获取下一个数据
        for (int i = 0; i < dataList.size() * 2; i++) {
            Long nextData = circularLinkedListLh.next();
            System.out.println(nextData);
        }
    }
}
